package com.xuelang.pipeline.artifact.app.resource.impl;

import com.xuelang.pipeline.artifact.app.harborv2.MemberService;
import com.xuelang.pipeline.artifact.app.harborv2.UserService;
import com.xuelang.pipeline.artifact.app.harborv2.request.CreateHarborUserRequest;
import com.xuelang.pipeline.artifact.app.harborv2.request.CreateMemberRequest;
import com.xuelang.pipeline.artifact.app.harborv2.request.SearchUserQuery;
import com.xuelang.pipeline.artifact.app.harborv2.response.SearchUserResponse;
import com.xuelang.pipeline.artifact.app.harborv2.xo.UserEntity;
import com.xuelang.pipeline.artifact.app.nexus3v1.SecurityUserService;
import com.xuelang.pipeline.artifact.app.nexus3v1.request.CreateUserRequest;
import com.xuelang.pipeline.artifact.app.nexus3v1.xo.ApiResponseXO;
import com.xuelang.pipeline.artifact.app.nexus3v1.xo.UserStatus;
import com.xuelang.pipeline.artifact.app.resource.InstanceUserService;
import com.xuelang.pipeline.artifact.client.common.constant.ArtifactConstant;
import com.xuelang.pipeline.artifact.client.common.exception.BizException;
import com.xuelang.pipeline.artifact.client.resource.enums.ArtifactToolEnum;
import com.xuelang.pipeline.artifact.domain.resource.ArtifactUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author xiwen.zxg
 * @since 2024/7/11
 */
@Service
public class InstanceUserServiceImpl implements InstanceUserService {

    @Autowired
    SecurityUserService securityUserService;
    @Autowired
    MemberService memberService;
    @Autowired
    UserService userService;

    @Override
    public void createUserOfRepository(ArtifactToolEnum.ArtifactTypeEnum artifactType, List<ArtifactUser> artifactUserList) {
        switch(artifactType) {
            case MAVEN:
            case NPM:
            case GENERIC:
                artifactUserList.stream().map(artifactUser -> {
                    CreateUserRequest createUserRequest = new CreateUserRequest();
                    createUserRequest.setUserId(artifactUser.getArtifactUserId());
                    createUserRequest.setFirstName(artifactUser.getInstanceRepositoryName());
                    createUserRequest.setLastName(artifactUser.getUserName());
                    //createUserRequest.setEmailAddress(artifactUser.getUserId() + ArtifactConstant.DEFAULT_EMAIL_SUFFIX);
                    createUserRequest.setEmailAddress(System.currentTimeMillis() + ArtifactConstant.DEFAULT_EMAIL_SUFFIX);
                    createUserRequest.setPassword(artifactUser.getArtifactUserPassword());
                    createUserRequest.setStatus(UserStatus.active);
                    createUserRequest.setRoles(Collections.singletonList(artifactUser.getArtifactRoleId()));
                    return securityUserService.createUser(createUserRequest);
                }).filter(createUserResponse -> !createUserResponse.isSuccess()).forEach(createUserResponse -> {
                    throw new BizException(createUserResponse.getMessage());
                });
                break;
            case DOCKER:
                artifactUserList.stream().map(artifactUser -> {
                    // 先创建用户
                    CreateHarborUserRequest createUserRequest = new CreateHarborUserRequest();
                    //createUserRequest.setEmail(artifactUser.getUserId() + ArtifactConstant.DEFAULT_EMAIL_SUFFIX);
                    createUserRequest.setEmail(System.currentTimeMillis() + ArtifactConstant.DEFAULT_EMAIL_SUFFIX);
                    createUserRequest.setRealname(artifactUser.getUserName());
                    createUserRequest.setComment(artifactUser.getArtifactRoleCode());
                    createUserRequest.setPassword(artifactUser.getArtifactUserPassword());
                    createUserRequest.setUsername(artifactUser.getArtifactUserId());
                    ApiResponseXO<Void> createUserResponse = userService.createUser(createUserRequest);
                    if (!createUserResponse.isSuccess()) {
                        return createUserResponse;
                    }

                    // 再创建项目和用户的关系
                    CreateMemberRequest createMemberRequest = new CreateMemberRequest();
                    createMemberRequest.setRoleId(Long.valueOf(artifactUser.getArtifactRoleId()));
                    createMemberRequest.setMemberUser(UserEntity.builder().username(artifactUser.getArtifactUserId()).build());
                    ApiResponseXO<SearchUserResponse> createMemberResponse = memberService.createProjectMember(artifactUser.getInstanceRepositoryName(), createMemberRequest);
                    if (!createMemberResponse.isSuccess()) {
                        return createMemberResponse;
                    }
                    return ApiResponseXO.success();
                }).filter(createMemberResponse -> !createMemberResponse.isSuccess()).forEach(createMemberResponse -> {
                    throw new BizException(createMemberResponse.getMessage());
                });
                break;
            default:
                throw new BizException("not support this type" + artifactType);
        }
    }



    @Override
    public void deleteUserOfRepository(ArtifactToolEnum.ArtifactTypeEnum artifactType, List<ArtifactUser> artifactUserList) {
        switch(artifactType) {
            case MAVEN:
            case NPM:
            case GENERIC:
                artifactUserList.stream().map(artifactUser -> securityUserService.deleteUser(artifactUser.getArtifactUserId())).filter(deleteUserResponse -> !deleteUserResponse.isSuccess()).forEach(deleteUserResponse -> {
                    throw new BizException(deleteUserResponse.getMessage());
                });
                break;
            case DOCKER:
                artifactUserList.stream().map(artifactUser -> {
                    ApiResponseXO<List<SearchUserResponse>> searchUserResponse = userService.searchUserByUserName(SearchUserQuery.builder().username(artifactUser.getArtifactUserId()).build());
                    if (searchUserResponse.isSuccess()
                            && searchUserResponse.getData() != null
                            && !searchUserResponse.getData().isEmpty()) {
                        searchUserResponse.getData().forEach(user -> userService.deleteUser(user.getUserID()));
                    }
                    return ApiResponseXO.success();
                }).filter(deleteUserResponse -> !deleteUserResponse.isSuccess()).forEach(deleteUserResponse -> {
                    throw new BizException(deleteUserResponse.getMessage());
                });
                break;
            default:
                throw new BizException("not support this type" + artifactType);
        }
    }

}
